/*
 * Class: GoldPrice
 * 
 * Created on Sep 16, 2013
 * 
 * (c) Copyright by Minh Tran
 * Global Cybersoft VN.
 * Floor 3-4-5, Helios Building, Quang Trung Software City
 */
package com.minh.tran.model;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.minh.tran.util.ItemUtil;

/**
 * The Class Gold.
 */
public class Gold extends Item
{

    /** The Constant FORMAT_DATE. */
    private static final String FORMAT_DATE = "MMM dd, yyyy HH:mm:ss a z";

    /** The price. */
    private double price;

    /** The date time. */
    private Date dateTime;

    /**
     * Instantiates a new gold.
     */
    public Gold(final String name)
    {
        super(name);
        this.price = 0;
    }

    /**
     * Instantiates a new gold.
     *
     * @param name the name
     * @param price the price
     */
    public Gold(final String name, final float price)
    {
        super(name);
        this.price = price;
    }

    /**
     * @return Returns the price.
     */
    public double getPrice()
    {
        return this.price;
    }

    /**
     * @param price The price to set.
     */
    public void setPrice(final double price)
    {
        this.price = price;
    }

    /**
     * @return Returns the dateTime.
     */
    public Date getDateTime()
    {
        return this.dateTime;
    }

    /**
     * @param dateTime The dateTime to set.
     */
    public void setDateTime(Date dateTime)
    {
        this.dateTime = dateTime;
    }

    @Override
    public String toString()
    {
        return ItemUtil.CHAR_OPEN + this.name + ItemUtil.CHAR_CLOSE;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (!(obj instanceof Gold))
        {
            return false;
        }
        final Gold other = (Gold) obj;
        if (other.name == null || this.name == null)
        {
            return false;
        }
        if (other.price < 0 || this.price < 0)
        {
            return false;
        }
        return other.name.equals(this.name) && (other.price == this.price);
    }

    /**
     * Sets the price.
     *
     * @param priceValue the new price
     */
    public void setPrice(final String priceValue)
    {
        if (priceValue != null)
        {
            try
            {
                this.price = Double.valueOf(priceValue);
            }
            catch (Exception e)
            {
            }
        }
    }

    /**
     * Sets the date time.
     *
     * @param dateValue the new date time
     */
    public void setDateTime(final String dateValue)
    {
        if (dateValue != null)
        {
            try
            {
                final DateFormat format = new SimpleDateFormat(FORMAT_DATE);
                final Date dateTime = format.parse(dateValue.trim());//Sep 16, 2013 11:54:51 AM EST
                this.dateTime = dateTime;
            }
            catch (Exception e)
            {
            }
        }
    }
}
